﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Modules;
using System.Web.UI.WebControls;
using System.IO;

namespace IB.Common.Base
{
    public class V_BaseSettings : ModuleSettingsBase
    {
        public string FolderList { get { return "Template"; } }
        public string FolderDetail { get { return "TemplateDetail"; } }
        public string FolderRelative { get { return "TemplateRelative"; } }

        public List<ListItem> LoadTypeList(string _folder,string _listType)
        {
            List<ListItem> lt = new List<ListItem>();
            string[] file = System.IO.Directory.GetFiles(Server.MapPath(ControlPath + "/"+_folder), "*.vc");
            string filename;
            for (int i = 0; i < file.Length; i++)
            {
                filename = file[i].Split('\\').Last();
                lt.Add(new ListItem { Text = filename.Split('.')[0], Value = filename, Selected = (filename == _listType) });
            }
            return lt;
        }

        public string CurrentTemplateContentList
        {
            get
            {
                return Settings["tempcontentlist"] == null ? "Chọn template" : Settings["tempcontentlist"].ToString();
            }
        }

        public string CurrentTemplateContentDetail
        {
            get
            {
                return Settings["tempcontentdetail"] == null ? "Chọn template" : Settings["tempcontentdetail"].ToString();
            }
        }

        public string CurrentTemplateContentRelative
        {
            get
            {
                return Settings["tempcontentrelative"] == null ? "Chọn template" : Settings["tempcontentrelative"].ToString();
            }
        }


        public string TemplateContent(string _folder,string filename)
        {
            string ctTemp = string.Empty;
            FileStream fs = new FileStream(Server.MapPath(ControlPath + "/"+_folder+"/") + filename + ".vc", FileMode.Open);
            try
            {
                StreamReader sr = new StreamReader(fs);
                ctTemp = sr.ReadToEnd();
            }
            finally { fs.Close(); }
            return ctTemp;
        }
    }
}
